var citysData = require('../../../data/citys-data.js');
var app = getApp();
Page({
  data: {
    isPlayingMusic: false
  },
  onLoad: function (option) {
    var cityId = option.id;
    this.data.currentcityId = cityId;
    var cityData = citysData.cityList[cityId];
    this.setData({
      cityData: cityData
    });

    var citysCollected = wx.getStorageSync('citys_collected');
    if (citysCollected) {
      var cityCollected = citysCollected[cityId];
      this.setData({
        collected: cityCollected
      });
    } else {
      var citysCollected = {};
      citysCollected[cityId] = false;
      wx.setStorageSync('citys_collected', citysCollected);
    }

    if (app.globalData.g_isPlayingMusic && app.globalData.g_currentMusiccityId === cityId) {
      this.setData({
        isPlayingMusic: true
      });
    }
    this.setMusicMonitor();
  },

  setMusicMonitor: function () {
    //点击播放图标和总控开关都会触发这个函数
    var that = this;
    wx.onBackgroundAudioPlay(function (event) {
      var pages = getCurrentPages();
      var currentPage = pages[pages.length - 1];
      if (currentPage.data.currentcityId === that.data.currentcityId) {
        // 打开多个city-detail页面后，每个页面不会关闭，只会隐藏。通过页面栈拿到到
        // 当前页面的cityid，只处理当前页面的音乐播放。
        if (app.globalData.g_currentMusiccityId == that.data.currentcityId) {
          // 播放当前页面音乐才改变图标
          that.setData({
            isPlayingMusic: true
          });
        }
        // if(app.globalData.g_currentMusiccityId == that.data.currentcityId )
        // app.globalData.g_currentMusiccityId = that.data.currentcityId;
      }
      app.globalData.g_isPlayingMusic = true;
    });
    wx.onBackgroundAudioPause(function () {
      var pages = getCurrentPages();
      var currentPage = pages[pages.length - 1];
      if (currentPage.data.currentcityId === that.data.currentcityId) {
        if (app.globalData.g_currentMusiccityId == that.data.currentcityId) {
          that.setData({
            isPlayingMusic: false
          });
        }
      }
      app.globalData.g_isPlayingMusic = false;
      // app.globalData.g_currentMusiccityId = null;
    });
    wx.onBackgroundAudioStop(function () {
      that.setData({
        isPlayingMusic: false
      });
      app.globalData.g_isPlayingMusic = false;
      // app.globalData.g_currentMusiccityId = null;
    });
  },

  onColletionTap: function (event) {
    // this.getcitysCollectedSyc();
    this.getcitysCollectedAsy();
  },

  getcitysCollectedAsy: function () {
    var that = this;
    wx.getStorage({
      key: 'citys_collected',
      success: function (res) {
        var citysCollected = res.data;
        var cityCollected = citysCollected[that.data.currentcityId];
        // 收藏变成未收藏，未收藏变成收藏
        cityCollected = !cityCollected;
        citysCollected[that.data.currentcityId] = cityCollected;
        that.showToast(citysCollected, cityCollected);
      }
    });
  },

  getcitysCollectedSyc: function () {
    var citysCollected = wx.getStorageSync('citys_collected');
    var cityCollected = citysCollected[this.data.currentcityId];
    // 收藏变成未收藏，未收藏变成收藏
    cityCollected = !cityCollected;
    citysCollected[this.data.currentcityId] = cityCollected;
    this.showToast(citysCollected, cityCollected);
  },

  showModal: function (citysCollected, cityCollected) {
    var that = this;
    wx.showModal({
      title: '收藏',
      content: cityCollected ? '收藏该文章？' : '取消收藏该文章？',
      showCancel: 'true',
      cancelText: '取消',
      cancelColor: '#333',
      confirmText: '确认',
      confirmColor: '#405f80',
      success: function (res) {
        if (res.confirm) {
          wx.setStorageSync('citys_collected', citysCollected);
          // 更新数据绑定变量，从而实现切换图片
          that.setData({
            collected: cityCollected
          });
        }
      }
    });
  },

  showToast: function (citysCollected, cityCollected) {
    // 更新文章是否的缓存值
    wx.setStorageSync('citys_collected', citysCollected);
    // 更新数据绑定变量，从而实现切换图片
    this.setData({
      collected: cityCollected
    });
    wx.showToast({
      title: cityCollected ? '收藏成功' : '取消成功',
      duration: 1000,
      icon: 'success'
    });
  },

  onShareTap: function (event) {
    var itemList = ['分享给微信好友', '分享到朋友圈', '分享到QQ', '分享到微博'];
    wx.showActionSheet({
      itemList: itemList,
      itemColor: '#405f80',
      success: function (res) {
        // res.cancel 用户是不是点击了取消按钮
        // res.tapIndex 数组元素的序号，从0开始
        wx.showModal({
          title: '用户 ' + itemList[res.tapIndex],
          content: ''
        });
      }
    });
  },

  onMusicTap: function (event) {
    var currentcityId = this.data.currentcityId;
    var cityData = citysData.cityList[currentcityId];
    var isPlayingMusic = this.data.isPlayingMusic;
    if (isPlayingMusic) {
      wx.pauseBackgroundAudio();
      this.setData({
        isPlayingMusic: false
      });
      // app.globalData.g_currentMusiccityId = null;
      app.globalData.g_isPlayingMusic = false;
    } else {
      wx.playBackgroundAudio({
        dataUrl: cityData.music.url,
        title: cityData.music.title,
        coverImgUrl: cityData.music.coverImg
      });
      this.setData({
        isPlayingMusic: true
      });
      app.globalData.g_currentMusiccityId = this.data.currentcityId;
      app.globalData.g_isPlayingMusic = true;
    }
  },

  /*
   * 定义页面分享函数
   */
  onShareAppMessage: function (event) {
    return {
      title: '瑞典',
      desc: '斯德哥尔摩',
      path: '/pages/citys/city-detail/city-detail?id=0'
    };
  }
});
